from sklearn import metrics
from sklearn.ensemble import RandomForestClassifier as RFC

from decorator import timeit
from preprocess.yyz_preprocess import YyzPreprocess

from sklearn.tree import DecisionTreeClassifier as DTC

from sklearn.neighbors import KNeighborsClassifier as KNN
from sklearn.ensemble import AdaBoostClassifier as ADA
from sklearn.ensemble import BaggingClassifier as BC
from sklearn.ensemble import GradientBoostingClassifier as GDBC
from sklearn.ensemble import RandomForestClassifier as RFC
from sklearn.naive_bayes import BernoulliNB as BLNB
from sklearn.naive_bayes import GaussianNB as GNB
from sklearn.svm import SVC

"""
项目程序入口
"""


def func(clf, x_train, y_train, x_test, y_test):
    clf.fit(x_train, y_train)
    y_pre = clf.predict(x_test)
    acc = metrics.precision_score(y_test, y_pre, average='micro')
    recall = metrics.recall_score(y_test, y_pre, average='micro')  # recall
    f1_score = metrics.f1_score(y_test, y_pre, average='weighted')
    return acc, f1_score


@timeit
def test_xy():
    X_train, y_train, X_test, y_test = YyzPreprocess().get_xy()

    # 决策树
    acc, F1_score = func(DTC(), X_train, y_train, X_test, y_test)
    print('决策树准确率为{:.2f}，F1值为{:.2f}'.format(acc, F1_score))

    # SVM
    acc, F1_score = func(SVC(), X_train, y_train, X_test, y_test)
    print('SVM准确率为{:.2f}，F1值为{:.2f}'.format(acc, F1_score))

    # KNN
    acc, F1_score = func(KNN(), X_train, y_train, X_test, y_test)
    print('KNN准确率为{:.2f}，F1值为{:.2f}'.format(acc, F1_score))

    # 随机森林
    acc, F1_score = func(RFC(), X_train, y_train, X_test, y_test)
    print('随机森林准确率为{:.2f}，F1值为{:.2f}'.format(acc, F1_score))

    # ADAboost
    acc, F1_score = func(ADA(n_estimators=20), X_train, y_train, X_test, y_test)
    print('Adaboost准确率为{:.2f},F1值为{:.2f}'.format(acc, F1_score))

    # GDBC
    acc, F1_score = func(GDBC(n_estimators=20), X_train, y_train, X_test, y_test)
    print('GDBT准确率为{:.2f}，F1值为{:.2f}'.format(acc, F1_score))

    # Bagging
    acc, F1_score = func(BC(n_estimators=20), X_train, y_train, X_test, y_test)
    print('Bagging准确率为{:.2f}，F1值为{:.2f}'.format(acc, F1_score))

    # 伯努利贝叶斯
    acc, F1_score = func(BLNB(), X_train, y_train, X_test, y_test)
    print('伯努利贝叶斯准确率为{:.2f}，F1值为{:.2f}'.format(acc, F1_score))

    # 高斯贝叶斯
    acc, F1_score = func(GNB(), X_train, y_train, X_test, y_test)
    print('高斯贝叶斯准确率为{:.2f}，F1值为{:.2f}'.format(acc, F1_score))


@timeit
def test_pca_xy():
    X_train, y_train, X_test, y_test = YyzPreprocess().get_pca_xy()

    # 决策树
    acc, F1_score = func(DTC(), X_train, y_train, X_test, y_test)
    print('决策树准确率为{:.2f}，F1值为{:.2f}'.format(acc, F1_score))

    # SVM
    acc, F1_score = func(SVC(), X_train, y_train, X_test, y_test)
    print('SVM准确率为{:.2f}，F1值为{:.2f}'.format(acc, F1_score))

    # KNN
    acc, F1_score = func(KNN(), X_train, y_train, X_test, y_test)
    print('KNN准确率为{:.2f}，F1值为{:.2f}'.format(acc, F1_score))

    # 随机森林
    acc, F1_score = func(RFC(), X_train, y_train, X_test, y_test)
    print('随机森林准确率为{:.2f}，F1值为{:.2f}'.format(acc, F1_score))

    # ADAboost
    acc, F1_score = func(ADA(n_estimators=20), X_train, y_train, X_test, y_test)
    print('Adaboost准确率为{:.2f},F1值为{:.2f}'.format(acc, F1_score))

    # GDBT
    acc, F1_score = func(GDBC(n_estimators=20), X_train, y_train, X_test, y_test)
    print('GDBT准确率为{:.2f}，F1值为{:.2f}'.format(acc, F1_score))

    # Bagging
    acc, F1_score = func(BC(n_estimators=20), X_train, y_train, X_test, y_test)
    print('Bagging准确率为{:.2f}，F1值为{:.2f}'.format(acc, F1_score))

    # 伯努利贝叶斯
    acc, F1_score = func(BLNB(), X_train, y_train, X_test, y_test)
    print('伯努利贝叶斯准确率为{:.2f}，F1值为{:.2f}'.format(acc, F1_score))

    # 高斯贝叶斯
    acc, F1_score = func(GNB(), X_train, y_train, X_test, y_test)
    print('高斯贝叶斯准确率为{:.2f}，F1值为{:.2f}'.format(acc, F1_score))


if __name__ == '__main__':
    test_xy()
    test_pca_xy()